package com.ruoyi.web.controller.publish;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.publish.domain.IdPublish;
import com.ruoyi.publish.domain.RvSample;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.publish.domain.IdLaw;
import com.ruoyi.publish.service.IIdLawService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 执法机构Controller
 * 
 * @author 张泽望
 * @date 2020-11-02
 */
@Controller
@RequestMapping("/publish/enforcement")
public class IdLawController extends BaseController
{
    private String prefix = "publish/enforcement";

    @Autowired
    private IIdLawService idLawService;

//    @RequiresPermissions("publish:enforcement:view")
    @GetMapping()
    public String enforcement()
    {
        return prefix + "/enforcement";
    }

    /**
     * 查询执法机构列表
     */
//    @RequiresPermissions("publish:enforcement:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(IdLaw idLaw)
    {
        startPage();
        List<IdLaw> list = idLawService.selectIdLawList(idLaw);
        return getDataTable(list);
    }

    /**
     * 导出执法机构列表
     */
//    @RequiresPermissions("publish:enforcement:export")
    @Log(title = "执法机构", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(IdLaw idLaw)
    {
        List<IdLaw> list = idLawService.selectIdLawList(idLaw);
        ExcelUtil<IdLaw> util = new ExcelUtil<IdLaw>(IdLaw.class);
        return util.exportExcel(list, "enforcement");
    }

    /**
     * 新增执法机构
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存执法机构
     */
//    @RequiresPermissions("publish:enforcement:add")
    @Log(title = "执法机构", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(IdLaw idLaw)
    {
        idLaw.setCreateBy(ShiroUtils.getLoginName());
        idLaw.setCreateName(ShiroUtils.getSysUser().getUserName());
        idLaw.setCreateDate(DateUtils.getNowDate());
        return toAjax(idLawService.insertIdLaw(idLaw));
    }

    /**
     * 修改执法机构
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        IdLaw idLaw = idLawService.selectIdLawById(id);
        mmap.put("idLaw", idLaw);
        return prefix + "/edit";
    }

    /**
     * 修改保存执法机构
     */
//    @RequiresPermissions("publish:enforcement:edit")
    @Log(title = "执法机构", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(IdLaw idLaw)
    {
        return toAjax(idLawService.updateIdLaw(idLaw));
    }

    /**
     * 删除执法机构
     */
//    @RequiresPermissions("publish:enforcement:remove")
    @Log(title = "执法机构", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(idLawService.deleteIdLawByIds(ids));
    }


    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {

        ExcelUtil<IdLaw> util = new ExcelUtil<IdLaw>(IdLaw.class);
        List<IdLaw> list = new ArrayList<>();
        IdLaw idLaw = new IdLaw();
        //执法机构
        idLaw.setDepartName("上海出版社(示范)");
        //管理人
        idLaw.setUserName("admin(示范)");
        list.add(idLaw);
        return util.importDataTemplateExcel(list,"执法机构数据");
    }

    @Log(title = "执法机构excel导入", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport, String year) throws Exception
    {
        byte[] bytes = file.getBytes();
        InputStream in = new ByteArrayInputStream(bytes);

        ExcelUtil<IdLaw> util = new ExcelUtil<IdLaw>(IdLaw.class);
        List<IdLaw> userList = util.importExcel(in);

        String operName = ShiroUtils.getSysUser().getLoginName();
        String message = idLawService.importIdLaw(userList, updateSupport, operName, year);
        return AjaxResult.success(message);
    }
}
